
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.ShopBillDao;
import com.sun.showcase.biz.mall.service.ShopBillService;
import com.sun.showcase.client.domain.mall.ShopBill;
import com.sun.showcase.client.query.mall.ShopBillQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("shopBillService")
@Transactional
public class ShopBillServiceImpl implements ShopBillService{
	@Autowired
	private ShopBillDao shopBillDao;
	
	public void setShopBillDao(ShopBillDao dao) {
		this.shopBillDao = dao;
	}

	public DataGrid datagrid(ShopBillQuery shopBillQuery) {
		DataGrid j = new DataGrid();
		Pager<ShopBill> pager  = find(shopBillQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<ShopBillQuery> getQuerysFromEntitys(List<ShopBill> shopBills) {
		List<ShopBillQuery> shopBillQuerys = new ArrayList<ShopBillQuery>();
		if (shopBills != null && shopBills.size() > 0) {
			for (ShopBill tb : shopBills) {
				ShopBillQuery b = new ShopBillQuery();
				BeanUtils.copyProperties(tb, b);
				shopBillQuerys.add(b);
			}
		}
		return shopBillQuerys;
	}

	private Pager<ShopBill> find(ShopBillQuery shopBillQuery) {
		Page<ShopBill> page = PageHelper.startPage(shopBillQuery.getPage().intValue(), shopBillQuery.getRows().intValue());
		List<ShopBill> list = shopBillDao.findList(shopBillQuery);
		Pager<ShopBill> pager = new Pager<ShopBill>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public ShopBill add(ShopBillQuery shopBillQuery) {
		ShopBill t = new ShopBill();
		BeanUtils.copyProperties(shopBillQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		shopBillDao.save(t);
		shopBillQuery.setId(t.getId());
		return t;
		
	}

	public void update(ShopBillQuery shopBillQuery) {
		ShopBill t = shopBillDao.getById(shopBillQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(shopBillQuery, t);
	    	shopBillDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(ShopBillQuery shopBillQuery) {
	    shopBillDao.updatePart(shopBillQuery);
	}

	public void delete(String[] ids) {
		shopBillDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		shopBillDao.deletePt(ids);
	}

	public ShopBill get(ShopBillQuery shopBillQuery) {
		return shopBillDao.getById(shopBillQuery.getId());
	}

	public ShopBill get(String id) {
		return shopBillDao.getById(id);
	}

	
	public List<ShopBillQuery> listAll(ShopBillQuery shopBillQuery) {
	    List<ShopBill> list = shopBillDao.findList(shopBillQuery);
		List<ShopBillQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public void updateStatus(String bid, String status) {
		ShopBillQuery up = new ShopBillQuery();
		up.setStatus(status);
		up.getSearchMap().put("bussiness_id", bid);
		this.updatePart(up);
	}
	
	
}
